package myapp.Chat;

import java.util.ArrayList;

import staticClass.XmppConnection;

import myapp.DTO.UserDTO;
import myapp.EnumAndConstans.Constants;
import myapp.googlemap.R;
import myapp.services.ChatServiceHelper;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

public class FriendListDialog extends Dialog implements android.view.View.OnClickListener{

	private ListView lvFriends = null;
	private XmppClient xmppClient;
	private ArrayList<UserDTO> friends;
	
	public FriendListDialog(XmppClient client) {
		super(client);
		// TODO Auto-generated constructor stub
		this.xmppClient = client;
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		setContentView(R.layout.chat_user_list_dialog);
		getWindow().setFlags(Constants.WINDOW_FLAG_FOR_DIALOG, Constants.WINDOW_MASKS_FOR_DIALOG);
        setTitle("Choose your recipient");
		
		InitializeListView();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
	
	private void InitializeListView(){
		Log.i("FriendListDialog", "[InitializeListView]");
		setItemClickListener();
		setYourFriendsToListView();
	}
	
	private void setItemClickListener(){
		Log.i("FriendListDialog", "[setItemClickListener]");
		lvFriends = (ListView) findViewById(R.id.lvUsers);
	}
	
	public void itemClickedCallback(UserDTO friend){
		Log.i("FriendListDialog", "[itemClickedCallback] called: name:"+friend.name);
		xmppClient.setNewRecipient(friend);
		dismiss();
	}
	
	private void setYourFriendsToListView(){
		Log.i("FriendListDialog", "[setYourFriendsToListView] start");
		ChatServiceHelper helper = new ChatServiceHelper();
		if (friends != null){
			friends.clear();
			friends = null;
		}
		
		friends = helper.getAllFriends(XmppConnection.userEmail, XmppConnection.hashPwd);
		//ArrayAdapter<UserDTO> adapter = new ArrayAdapter<UserDTO>(getContext(), R.layout.multi_line_list_item, friends);
		FriendListAdapter adapter = new FriendListAdapter(getContext(), R.layout.chat_user_info_item_list, friends, this);
		lvFriends.setAdapter(adapter);
		Log.i("FriendListDialog", "[setYourFriendsToListView] done");
	}

}
